package com.xbx.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.xbx.commonutils.R;
import com.xbx.eduservice.entity.EduTeacher;
import com.xbx.eduservice.entity.vo.TeacherQuery;
import com.xbx.eduservice.service.EduTeacherService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.management.Query;
import java.sql.Wrapper;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-12-09
 */
@RestController
@RequestMapping("/eduservice/edu-teacher")
@CrossOrigin
public class EduTeacherController {
    /*1.查询teacher表中的所有数据啊*/
    @Autowired
    private EduTeacherService eduTeacherService;

    @GetMapping("/findAll")
    public R findAll() {
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items", list);
    }

    /*2.这个是逻辑删除的方法*/
    @DeleteMapping("{id}")
    public R deleteByid(@PathVariable String id) {
        Boolean flag = eduTeacherService.removeById(id);
        if (flag) {
            return R.ok().data("result", flag);
        } else {
            return R.error().data("result", flag);
        }
    }

    /*3.分页查询讲师的方法*/
    @GetMapping("getteacher/{current}/{limit}")
    public R getteacher(@PathVariable long current,
                        @PathVariable long limit
    ) {
        /*新增page对象，将当前页和查询页码传入*/
        Page<EduTeacher> eduteacher = new Page<>(current, limit);
        /*调用service层的page方法，将上访page对象和限制条件传入，所查询的数据继续封装到page对象中*/
        eduTeacherService.page(eduteacher, null);
        /*取出page对象的total数据*/
        Long total = eduteacher.getTotal();
        /*取出page对象的records数据，是list数组*/
        List<EduTeacher> result = eduteacher.getRecords();
        return R.ok().data("total", total).data("items", result);
    }

    /*4.条件查询带分页的方法*/
    @PostMapping("PageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current,
                                  @PathVariable long limit,
            /*该注解的含义在于：前端传回的数据可以直接封装至相应对象中，并且只能使用post请求方式，且在必要时指定required值*/
                                  @RequestBody(required = false) TeacherQuery teacherQuery) {
        /*新建page对象，传入current和limit*/
        Page<EduTeacher> eduteacher = new Page<>(current, limit);
        /*新建条件封装对象，指定类型*/
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        /*添加判断条件，传入参数分别为db字段名和参数*/
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.gt("begin", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("end", end);
        }
        /*调用service分页查询，给定page对象和wrapper查询条件*/
        eduTeacherService.page(eduteacher, wrapper);

        /*封装结果，返回*/
        return R.ok().data("items", eduteacher);
    }

    /*5.新增讲师功能*/
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduteacher) {
        Boolean result = eduTeacherService.save(eduteacher);
        if (result) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    /*6.修改讲师功能*/
    @GetMapping("getTeacher/{id}")
    /*根据id将教师信息查询出来*/
    public R getTeacherById(@PathVariable long id){
        EduTeacher eduteacher=eduTeacherService.getById(id);
        return R.ok().data("teacher",eduteacher);
    }
    /*根据给出的教师信息进行对应修改*/
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        Boolean result=eduTeacherService.updateById(eduTeacher);
        if(result){
            return R.ok();
        }else {
            return R.error();
        }
    }
}

